$(document).ready(function() {
			updateMainBlock();
		});

function getTitles(type, currentPage, pageSize) {
	window.location.href = "#type=" + type + "&currentPage=" + currentPage
			+ "&pageSize=" + pageSize;
	var updateNews = function(titles) {
		if (titles.length == 0) {
			$("#newsList").html("NO RECORD");
		} else {
			$("#newsList").html("");
			for (var i = 0; i < titles.length; i++) {
				$("#newsList")
						.append("<li><div><a href='javascript:void(0)' onClick='getArticle("
								+ titles[i].id
								+ ")'>"
								+ titles[i].title
								+ "</a></div></li>");
			}
			$("#newsList li:even").css("background-color", "#E8F2FE");
		}
	};
	var updatePage = function(page) {
		if (page.lineCount == 0) {
			$("#col0-list2").html("");
			return;
		}
		$("#currentPage").html(page.currentPage);
		$(".page-num").html("");
		if (page.totalPages < 5) {
			for (var i = 1; i <= page.totalPages; i++) {
				$(".page-num")
						.append("<a href='javascript:void(0)' onClick='getTitles("
								+ type + "," + i + " , 10)'>" + i + "</a>");
			}
		} else if (page.currentPage < 3) {
			$(".page-num")
					.append("<a href='javascript:void(0)' onClick='getTitles("
							+ type + "," + 1 + " , 10)'>1</a>");
			$(".page-num")
					.append("<a href='javascript:void(0)' onClick='getTitles("
							+ type + "," + 2 + " , 10)'>2</a>");
			$(".page-num")
					.append("<a href='javascript:void(0)' onClick='getTitles("
							+ type + "," + 3 + " , 10)'>3</a>");
			$(".page-num").append("<a>...</a>");
			$(".page-num")
					.append("<a href='javascript:void(0)' onClick='getTitles("
							+ type + "," + page.totalPages + " , 10)'>"
							+ page.totalPages + "</a>");
		} else if (page.currentPage + 2 > page.totalPages) {
			$(".page-num")
					.append("<a href='javascript:void(0)' onClick='getTitles("
							+ type + "," + 1 + " , 10)'>1</a>");
			$(".page-num").append("<a>...</a>");
			$(".page-num")
					.append("<a href='javascript:void(0)' onClick='getTitles("
							+ type + "," + (page.totalPages - 2) + " , 10)'>"
							+ (page.totalPages - 2) + "</a>");
			$(".page-num")
					.append("<a href='javascript:void(0)' onClick='getTitles("
							+ type + "," + (page.totalPages - 1) + " , 10)'>"
							+ (page.totalPages - 1) + "</a>");
			$(".page-num")
					.append("<a href='javascript:void(0)' onClick='getTitles("
							+ type + "," + page.totalPages + " , 10)'>"
							+ page.totalPages + "</a>");
		} else if (page.currentPage == 3) {
			$(".page-num")
					.append("<a href='javascript:void(0)' onClick='getTitles("
							+ type + "," + 1 + " , 10)'>1</a>");
			$(".page-num")
					.append("<a href='javascript:void(0)' onClick='getTitles("
							+ type + "," + 2 + " , 10)'>2</a>");
			$(".page-num")
					.append("<a href='javascript:void(0)' onClick='getTitles("
							+ type + "," + 3 + " , 10)'>3</a>");
			$(".page-num")
					.append("<a href='javascript:void(0)' onClick='getTitles("
							+ type + "," + 4 + " , 10)'>4</a>");
			if (page.totalPages == 5) {
				$(".page-num")
						.append("<a href='javascript:void(0)' onClick='getTitles("
								+ type + "," + 5 + " , 10)'>5</a>");
			} else {
				$(".page-num").append("<a>...</a>");
				$(".page-num")
						.append("<a href='javascript:void(0)' onClick='getTitles("
								+ type
								+ ","
								+ page.totalPages
								+ " , 10)'>"
								+ page.totalPages + "</a>");
			}
		} else if (page.currentPage + 2 == page.totalPages) {
			$(".page-num")
					.append("<a href='javascript:void(0)' onClick='getTitles("
							+ type + "," + 1 + " , 10)'>1</a>");
			if (page.totalPages == 5) {
				$(".page-num")
						.append("<a href='javascript:void(0)' onClick='getTitles("
								+ type
								+ ","
								+ (page.totalPages - 3)
								+ " , 10)'>" + (page.totalPages - 3) + "</a>");
			} else {
				$(".page-num").append("<a>...</a>");
				$(".page-num")
						.append("<a href='javascript:void(0)' onClick='getTitles("
								+ type
								+ ","
								+ (page.totalPages - 3)
								+ " , 10)'>" + (page.totalPages - 3) + "</a>");
			}
			$(".page-num")
					.append("<a href='javascript:void(0)' onClick='getTitles("
							+ type + "," + (page.totalPages - 2) + " , 10)'>"
							+ (page.totalPages - 2) + "</a>");
			$(".page-num")
					.append("<a href='javascript:void(0)' onClick='getTitles("
							+ type + "," + (page.totalPages - 1) + " , 10)'>"
							+ (page.totalPages - 1) + "</a>");
			$(".page-num")
					.append("<a href='javascript:void(0)' onClick='getTitles("
							+ type + "," + page.totalPages + " , 10)'>"
							+ page.totalPages + "</a>");
		} else {
			$(".page-num")
					.append("<a href='javascript:void(0)' onClick='getTitles("
							+ type + "," + 1 + " , 10)'>1</a>");
			$(".page-num").append("<a>...</a>");
			$(".page-num")
					.append("<a href='javascript:void(0)' onClick='getTitles("
							+ type + "," + (page.currentPage - 1) + " , 10)'>"
							+ (page.currentPage - 1) + "</a>");
			$(".page-num")
					.append("<a href='javascript:void(0)' onClick='getTitles("
							+ type + "," + page.currentPage + " , 10)'>"
							+ page.currentPage + "</a>");
			$(".page-num")
					.append("<a href='javascript:void(0)' onClick='getTitles("
							+ type + "," + (page.currentPage + 1) + " , 10)'>"
							+ (page.currentPage + 1) + "</a>");
			$(".page-num").append("<a>...</a>");
			$(".page-num")
					.append("<a href='javascript:void(0)' onClick='getTitles("
							+ type + "," + page.totalPages + " , 10)'>"
							+ page.totalPages + "</a>");
		}
	}
	$.getJSON("../ShowTitle", {
				type : type,
				currentPage : currentPage,
				pageSize : pageSize
			}, function(result) {
				updateNews(result.titles);
				updatePage(result.page);
			});
}

function getArticle(id) {
	var updateArticle = function(json) {
		window.location.href = "#id=" + id;
		$("#col0-list1").html("<p class='wpy-title'>" + json.title + "</p>");
		$("#col0-list2")
				.html("<p class='wpy-text'>"
						+ json.content
						+ "</p><br /><p><a href='news.jsp' style='color:#003399'>Back</a></p>");
	};
	$.getJSON("../ShowArticle", {
				id : id
			}, function(result) {
				updateArticle(result);
			});
}

/**
 * 根据域名进行更新
 */
function updateMainBlock() {
	var url = window.location.href;
	var startIndex = url.indexOf("#") + 1;
	if (startIndex == 0) {
		getTitles(1, 1, 10);
	}
	var subUrl = url.substring(startIndex);
	if (subUrl.indexOf("type") == 0) {
		var params = subUrl.split("&");
		var type = params[0].substring(params[0].search("[0-9]+"));
		var currentPage = params[1].substring(params[1].search("[0-9]+"));
		var pageSize = params[2].substring(params[2].search("[0-9]+"));
		getTitles(type, currentPage, pageSize);
	} else if (subUrl.indexOf("id") == 0) {
		var id = subUrl.substring(subUrl.search("[0-9]+"));
		getArticle(id);
	}
}